//
//  PMineResumeLanguageViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/24.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineResumeLanguageViewController.h"

@interface PMineResumeLanguageViewController ()<TimeSelectPickerViewDelegate, PSSingleRowViewDelegate>
{
    BaseNetManager *netManager;
    
    PSCell *languageCell;
    PSCell *levelCell;
    
    NSString *languageID;
    NSString *levelID;
}
@property (nonatomic, strong) PSSingleRowView *languagePickerView;
@property (nonatomic, strong) PSSingleRowView *levelPickerView;
@end

@implementation PMineResumeLanguageViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.languagePickerView) {
        self.languagePickerView.hidden = YES;
    }
    if (self.levelPickerView) {
        self.levelPickerView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"语言能力";
    netManager = [BaseNetManager sharedManager];
    
    if (self.type == AddOrEditTypeOfEdit) {
        [self loadData];
    } else {
        self.model = [[PMineResumeLanguageInfoModel alloc] init];
        [self initUI];
    }
}
- (void)loadData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:self.model.info_id forKey:@"info_id"];
    
    [BaseNetManager requestWithMethod:POST path:PersonMine_GetLanguageInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                self.model = [[PMineResumeLanguageInfoModel alloc] init];
                [self.model setValuesForKeysWithDictionary:data[0]];
            } else if ([data isKindOfClass:[NSDictionary class]]) {
                self.model = [[PMineResumeLanguageInfoModel alloc] init];
                [self.model setValuesForKeysWithDictionary:data];
            }
            self->languageID = self.model.language;
            self->levelID = self.model.level;
            [self initUI];
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        [self loadData];
    }];
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36) imageName:nil target:self action:@selector(okBtnTap) title:((self.type == AddOrEditTypeOfAdd)?@"确定":@"保存") titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    __weak __typeof(self)weakSelf = self;
    self.languagePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.languageArray target:self];
    self.languagePickerView.hidden = YES;
    self.levelPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.languageLevelArray target:self];
    self.levelPickerView.hidden = YES;
    
    languageCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, 10*Scale, WIDTH-60*Scale, PSCellHeight) title:@"语种" text:((self.model&&self.model.language_cn.length)?self.model.language_cn:nil) placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    languageCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.languagePickerView.hidden = NO;
    };
    [self.view addSubview:languageCell];
    
    levelCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(languageCell), WIDTH-60*Scale, PSCellHeight) title:@"熟悉程度" text:((self.model&&self.model.level_cn.length)?self.model.level_cn:nil) placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    levelCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.levelPickerView.hidden = NO;
    };
    [self.view addSubview:levelCell];
    
    if (self.type == AddOrEditTypeOfEdit) {
        UIButton *deleteBtn = [UIToolsControl initButtonWithFrame:CGRectMake(40*Scale, EndY(levelCell)+30*Scale, WIDTH-80*Scale, 40*Scale) imageName:nil target:self action:@selector(deleteBtnTap) title:@"删除此条语种" titleColor:COLOR_OF_WHITE];
        deleteBtn.backgroundColor = THEMECOLOR_OF_RED;
        deleteBtn.layer.cornerRadius = 20*Scale;
        deleteBtn.layer.masksToBounds = YES;
        deleteBtn.titleLabel.font = Font(Font_17);
        [self.view addSubview:deleteBtn];
    }
}
- (void)okBtnTap {
    if (languageCell.textField.text.length) {
        if (levelCell.textField.text.length) {
            SVPHUDShow;
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
            dic = [UIToolsControl getPublicParameter];
            [dic setObject:((self.model && self.model.info_id.length)?self.model.info_id:@"") forKey:@"info_id"];
            [dic setObject:languageID forKey:@"language"];
            [dic setObject:levelID forKey:@"level"];
            
            [BaseNetManager requestWithMethod:POST path:PersonMine_SaveLanguageInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
                if ([responseObject[@"code"] integerValue] == 2000) {
                    SVPHUDSuccess(responseObject[@"msg"]);
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(pMineResumeLanguageViewControllerBackRefresh:)]) {
                        [self.delegate pMineResumeLanguageViewControllerBackRefresh:YES];
                    }
                    
                    [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
                } else {
                    SVPHUDError(responseObject[@"msg"]);
                }
            } failurBlock:^(NSError * _Nonnull error) {
                SVPHUDError(NetErrorAlertText);
            }];
        } else {
            SVPHUDError(LanguageLevelAlertText);
        }
    } else {
        SVPHUDError(LanguageAlertText);
    }
}
- (void)delayClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)psSingleRowViewCancel:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
}
- (void)psSingleRowViewOk:(PublicCategoryElementModel *)model PSSingleRowView:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
    if (psSingleRowView == self.languagePickerView) {
        languageCell.textField.text = model.name;
        languageID = model.id;
    } else {
        levelCell.textField.text = model.name;
        levelID = model.id;
    }
}
- (void)deleteBtnTap {
    CustomView *alertView = [[CustomView alloc] initWithTitle:@"提示" message:@"确认删除此条语种" prompt:nil sureBtn:@"确定" cancleBtn:@"取消" size:150];
    alertView.resultIndex = ^(NSInteger index){
        //回调---处理一系列动作
        if (index == 2) {
            [self deleteClick];
        }
    };
    [alertView showXLAlertView];
}
- (void)deleteClick {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:self.model.info_id forKey:@"info_id"];
    
    [BaseNetManager requestWithMethod:POST path:PersonMine_DelLanguageInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(pMineResumeLanguageViewControllerBackRefresh:)]) {
                [self.delegate pMineResumeLanguageViewControllerBackRefresh:YES];
            }
            [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

@end
